var tb = Vue.prototype.dbs;
var tablename = tb;
var tablepath = '_doc/' + tb + '.db'
var tbname = tb
let _sql = {

  // 打开数据库
  async openDB() {

    // #ifdef APP-PLUS
    let options = {
      name: tablename,
      path: tablepath
    }
    if (!plus.sqlite.isOpenDatabase(options)) {
      plus.sqlite.openDatabase({
        name: tablename,
        path: tablepath,
        success: async (res) => {
          await this.executeSQL()
        },
        fail: function(e) {
          this.openDB()
        }
      });
    } else {
      //this.executeSQL()
      //this.deleteSQL() 
      console.log('数据库已打开')
    }
    // #endif
  },
  // 执行SQL语句 

  // CREATE TABLE IF NOT EXISTS bookt(
  //     `id` INT(4) NOT NULL AUTO_INCREMENT COMMENT '书源id',
  //     `book_state` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '是否启用状态',
  //     `book_name` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源名称',
  //     `book_url` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源网址',
  //     `search_url` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源搜索url规则',
  //     `rand_url` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源内容url规则',
  //      `body_url` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源内容url规则',
  //     `book_group` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源分组',
  //     `book_data` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源书籍内容',
  //     `search_name` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源搜索名字',
   //     `search_http` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源搜索请求方式',
  //     `search_img` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源搜索图片',
  //     `search_author` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源搜索作者',
  //     `search_sort` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源搜索类别',
  //     `search_introduce` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源搜索介绍',
  //     `search_chapter` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源搜索章节',
  //     `rand_name` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源书籍名字',
  //     `rand_img` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源书籍图片',
  //     `rand_author` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源书籍作者',
  //     `rand_sort` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源书籍分类',
  //     `rand_state` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源书籍状态',
  //     `rand_time` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源书籍时间',
  //     `rand_link` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源书籍地址',
  //     `rand_intro` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源书籍描述',
  //     `rand_content` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源书籍规则区域',
  //     `rand_id` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源书籍id',
  //     `zhang_id` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源章节id',
  //     `zhang_content` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源章节区域规则',
  //     `zhang_name` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源章节名称规则',
  //     `zhang_link` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源章节链接规则',
  //     `zhang_list` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源章节列表规则',
  //     `search_content` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源搜索区域规则',
  //     `book_id` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '书源搜索区域规则',
   //     `search_gz` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '搜索每行结束规则',
    //     `img_url` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '是否设置图片域名',book_body body_furl
  //     PRIMARY KEY(`id`)
  // )
  async executeSQL() { //创建表
    var _this = this
    plus.sqlite.executeSql({
      name: tablename,
      sql: "CREATE TABLE IF NOT EXISTS " + tbname +
        " (`id` INTEGER PRIMARY KEY AUTOINCREMENT,`book_state` INT,`book_name` TEXT,`book_regular` TEXT,`book_limit` TEXT,`book_page` TEXT,`book_url` TEXT,`book_group` TEXT,`book_data` TEXT,`search_name` TEXT,`search_url` TEXT,`rand_url` TEXT,`body_url` TEXT,`search_http` TEXT,`search_img` TEXT,`search_author` TEXT,`search_gz` TEXT,`search_sort` TEXT,`search_introduce` TEXT,`search_chapter` TEXT,`rand_name` TEXT,`rand_img` TEXT,`rand_author` TEXT,`rand_sort` TEXT,`rand_state` TEXT,`rand_time` TEXT,`rand_link` TEXT,`rand_intro` TEXT,`rand_content` TEXT,`rand_id` TEXT,`zhang_id` TEXT,`zhang_content` TEXT,`zhang_list` TEXT,`zhang_name` TEXT,`zhang_link` TEXT,`search_content` TEXT,`book_id` TEXT,`header` TEXT,`img_url` TEXT,`zhang_url` TEXT,`book_body` TEXT,`body_furl` TEXT); DEFAULT `CHARSET`=utf8",
      success: async (res) => {
        await this.openDB()
        console.log('创建成功!');
      },
      fail: function(e) {
        console.log('创建失败: ' + JSON.stringify(e));
      }
    });
  },
  deleteSQL() { //删除表
    plus.sqlite.executeSql({
      name: tablename,
      sql: "drop table if exists " + tbname + "",
      success: function(e) {
        console.log('删除成功!');
      },
      fail: function(e) {
        console.log('executeSql failed: ' + JSON.stringify(e));
      }
    });
  },
  async Update(data, id) {

    for (var i in data) {

      if (/(?:\")/.test(data[i])) {
        data[i] = '\'' + data[i] + '\''
      } else if (/(?:\')/.test(data[i])) {
        data[i] = "\"" + data[i] + "\""
      } else {
        data[i] = data[i] ? '\'' + data[i] + '\'' : '\""'
      }
    }
    var sql = 'update ' + tbname + '  SET book_state = ' + data.book_state +' , book_page=' + data.book_page +' , book_limit=' + data.book_limit + ' , book_name=' + data.book_name +',book_url=' + data.book_url + ',search_url=' + data.search_url + ',rand_url=' + data.rand_url + ',body_url=' + data.body_url + ',book_group=' + data.book_group + ',book_data=' + data.book_data +',search_gz=' + data.search_gz +
      ',search_name=' + data.search_name + ',search_img=' + data.search_img + ',search_author=' + data.search_author + ',search_sort=' + data.search_sort + ',search_introduce=' + data.search_introduce+ ',search_http=' + data.search_http +
      ',search_chapter=' + data.search_chapter + ',rand_name=' + data.rand_name + ',rand_img=' + data.rand_img +
      ',rand_author=' + data.rand_author + ',rand_sort=' + data.rand_sort + ',rand_state=' + data.rand_state +
      ',rand_time=' + data.rand_time + ',rand_link=' + data.rand_link + ',rand_intro=' + data.rand_intro +
      ',rand_content=' + data.rand_content + ',rand_id=' + data.rand_id + ',zhang_id=' + data.zhang_id +',header='+data.header+
      ',zhang_content=' + data.zhang_content + ',zhang_name=' + data.zhang_name + ',zhang_link=' + data.zhang_link +
      ',search_content=' + data.search_content + ',zhang_url=' + data.zhang_url+ ',book_id=' + data.book_id+  ',img_url=' + data.img_url + ',zhang_list=' + data.zhang_list+  ',body_furl=' + data.body_furl +  ',book_body=' + data.book_body +',book_regular=' + data.book_regular +' where id=' + id + ''
    return new Promise((resolve, reject) => {
      plus.sqlite.executeSql({
        name: tablename,
        sql: sql,
        success(e) {
          resolve(true);
        },
        fail(e) {
          reject(false);
        },
      });
    });
  },
  async insertSQL(data) { // var data =JSON.parse(data.search_name book_body)
    for (var i in data) {

      if (/(?:\")/.test(data[i])) {
        data[i] = '\'' + data[i] + '\''
      } else if (/(?:\')/.test(data[i])) {
        data[i] = "\"" + data[i] + "\""
      } else {
        data[i] = data[i] ? '\'' + data[i] + '\'' : '\""'
      }
    }
    var sql = "insert into " + tbname +
      "(book_state,book_name,book_url,book_group,book_data,search_name,search_img,search_author,search_sort,search_introduce,search_chapter,rand_name,rand_img,rand_author,rand_sort,rand_state,rand_time,rand_link,rand_intro,rand_content,rand_id,zhang_id,zhang_content,zhang_name,zhang_link,search_content,book_id,zhang_list,search_url,body_url,rand_url,search_gz,search_http,book_limit,book_page,book_regular,header,img_url,zhang_url,book_body,body_furl) values (" +
      data.book_state + ',' + data.book_name + ',' + data.book_url + ',' + data.book_group + ',' + data.book_data +
      ',' + data.search_name + ',' + data.search_img + ',' + data.search_author + ',' + data.search_sort + ',' +
      data.search_introduce + ',' + data.search_chapter + ',' + data.rand_name + ',' + data.rand_img + ',' + data.rand_author + ',' + data.rand_sort + ',' + data.rand_state + ',' + data.rand_time + ',' + data.rand_link +
      ',' + data.rand_intro + ',' + data.rand_content + ',' + data.rand_id + ',' + data.zhang_id + ',' + data.zhang_content + ',' + data.zhang_name + ',' + data.zhang_link + ',' + data.search_content + ',' + data.book_id +',' + data.zhang_list +  ',' + data.search_url +',' + data.body_url +',' + data.rand_url +',' + data.search_gz +',' + data.search_http+',' + data.book_limit+',' + data.book_page+',' + data.book_regular+',' + data.header+',' + data.img_url+',' + data.zhang_url+',' + data.book_body+',' + data.body_furl+")"
    return new Promise((resolve, reject) => {
      plus.sqlite.executeSql({ 
        name: tablename,
        sql: sql,
        success(e) {
          resolve(true);
        },
        fail(e) {
          reject(false);
        },
      });
    });
  },
  async selectSQL(sql) { //查询
    return new Promise((resolve, reject) => {
      plus.sqlite.selectSql({
        name: tablename,
        sql: sql,
        success: function(e) {

          resolve(e);
        },
        fail: function(e) {
          console.log('数据库查询错误', e)
          reject(e);
        }
      });
    })
  },
  delSQL(sql) { //查询
    return new Promise((resolve, reject) => {
      plus.sqlite.executeSql({
        name: tablename,
        sql: sql,
        success: function(e) {
          resolve(true);
        },
        fail: function(e) {
          // console.log('数据库查询错误', e)
          reject(true);
        }
      });
    })
  }


}
export default _sql;
